//
//  BGMHTTP.m
//  BGM
//
//  Created by liuqin on 2018/11/28.
//  Copyright © 2018年 com.alirtc.demo. All rights reserved.
//

#import "BGMHTTP.h"
#import "AFHTTPSessionManager.h"
#import "Common.h"
#import "BGMUser.h"
#import "SVProgressHUD.h"


@implementation BGMHTTP



+ (void)requestWithMethodName:(NSString *)method parameters:(NSDictionary *)params completion:(void(^)(BOOL success,NSError *error,NSDictionary *result))completed{
    
    NSString *urlString = [NSString stringWithFormat:@"%@%@",Server_url,method];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
 
    
 
    
    [manager POST:urlString parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [SVProgressHUD dismiss];
        NSDictionary *dic = responseObject;
        if (completed) {
            NSString *code = [NSString stringWithFormat:@"%@",[dic objectForKey:@"code"]];
            if ([code isEqualToString:@"200"] ||[code isEqualToString:@"300"] ) {
                completed(YES,nil,dic);
            }else if ([code isEqualToString:@"401"]){
                completed(YES,nil,dic);
            }else if ([code isEqualToString:@"403"]){
                completed(YES,nil,dic);
            }
            else if ([code isEqualToString:@"400"]){
                NSLog(@">>>>>>>需要重新登录<<<<<<<<<<<");
            }
            else{
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"code = %@ 错误    %@",code,[responseObject objectForKey:@"desc"]]];
            }
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (completed) {
            completed(NO,error,nil);
        }
        NSLog(@"----------%@ = %@--------",@"请求错误",task.currentRequest.URL);
        [SVProgressHUD showErrorWithStatus:@"连接服务器错误"];

    }];
    
    
    
    
//    
//    [manager POST:urlString parameters:params headers:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
//        NSLog(@"");
//    } progress:^(NSProgress * _Nonnull uploadProgress) {
//        
//    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        [SVProgressHUD dismiss];
//        NSDictionary *dic = responseObject;
//        if (completed) {
//            NSString *code = [NSString stringWithFormat:@"%@",[dic objectForKey:@"code"]];
//            if ([code isEqualToString:@"200"] ||[code isEqualToString:@"300"] ) {
//                completed(YES,nil,dic);
//            }else if ([code isEqualToString:@"401"]){
//                completed(YES,nil,dic);
//            }else if ([code isEqualToString:@"403"]){
//                completed(YES,nil,dic);
//            }
//            else if ([code isEqualToString:@"400"]){
//                NSLog(@">>>>>>>需要重新登录<<<<<<<<<<<");
//            }
//            else{
//                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"code = %@ 错误    %@",code,[responseObject objectForKey:@"data"]]];
//            }
//        }
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        if (completed) {
//            completed(NO,error,nil);
//        }
//         NSLog(@"----------%@ = %@--------",@"请求错误",task.currentRequest.URL);
//        [SVProgressHUD showErrorWithStatus:@"连接服务器错误"];
//
//    }];
    
}

@end
